let gulp = require("gulp");

let { paths , proxyList } = require("./gulp.config")
let connect = require("gulp-connect")
let proxy = require("http-proxy-middleware")



function devConfig(){
    function devStyles(){
        return gulp.src(paths.styles.src)
        .pipe(gulp.dest(paths.styles.dist))
        .pipe(connect.reload());
    }
    
    function devScripts(){
        return gulp.src(paths.scripts.src)
        .pipe(gulp.dest(paths.scripts.dist))
        .pipe(connect.reload());
    }
    
    function devHtml(){
        return gulp.src(paths.html.src)
        .pipe(gulp.dest(paths.html.dist))
        .pipe(connect.reload());
    }
    
    
    function useconnect(){
        return connect.server({
            root : "./src",
            port : 8888,
            livereload : true,
            middleware : function(connect , opt){
                // return [
                //     proxy("/baidu",
                //     {target:"http://www.baidu.com",changeOrigin : true,
                //     pathRewrite : {
                //         "^/baidu" : ""
                //     }
                // }),
                // proxy("/zc" , {
                //     target : "https://www.zcool.com.cn/recommend/contentRecommend?pageSize=5&contentId=9196032_3&day=2018-12-04",
                //     changeOrigin : true,
                //     pathRewrite : {
                //         "^/zc" : ""
                //     }
                // })
                // ]
                let arr = [];
                for(let attr in proxyList){
                    arr.push(proxy(attr,proxyList[attr]))
                }
                return arr;
            }
        })
    }
    
    function watch(){
        gulp.watch(paths.styles.src,devStyles)
        gulp.watch(paths.scripts.src,devScripts)
        gulp.watch(paths.html.src,devHtml)
    }
    
    let dev = gulp.series(gulp.parallel( useconnect , watch));
    return dev;    
}

// exports.dev = dev;
module.exports = devConfig;